package com.mobartisan.workinglunch.util;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.IOException;

/**
 * Created by dell on 2016/10/3.
 */
public class FileUtil {
    private static final String CAPTURE_FILE_PRIX = "Capture_";
    private static final String HEAD_PRIX = "Head_";
    private static final String IMG_CACHE_PRIX = "Img_cache_";
    private static final String JPG = ".jpg";

    /**
     * 获取缓存头像路径
     *
     * @param context
     * @return
     */
    public static String getHeadFilePath(Context context) {
        String outpath = getStoragePathIfMounted(context, "sainty/head");
        String headFilename = outpath + "/" + HEAD_PRIX + JPG;//图片名字
        File headfile = new File(headFilename);
        try {
            if (!headfile.exists())
                headfile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return headFilename;
    }

    public static File getCaptureFile(Context context) {
        String name = CAPTURE_FILE_PRIX + System.currentTimeMillis() + JPG;
        File captureFile = new File(getStoragePathIfMounted(context, "sainty/Capture"), name);
        return captureFile;
    }

    /**
     * 获取图片缓存根目录
     *
     * @param context
     * @return
     */
    public static String getCacheImgPath(Context context) {
        String outpath = getStoragePathIfMounted(context, "sainty/Img");
        String imgFilename = outpath + "/" + IMG_CACHE_PRIX + System.currentTimeMillis() + JPG;//图片名字
        return imgFilename;
    }

    /**
     * 获取应用保存的位置
     *
     * @param context
     * @return
     */
    public static String getStoragePathIfMounted(Context context, String name) {
        File dir = context.getFilesDir();
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            dir = Environment.getExternalStorageDirectory();
        }
        File storageFolder = buildPath(dir, name);
        if (!storageFolder.exists()) {
            storageFolder.mkdirs();
        }
        Log.d("storage path", storageFolder.getAbsolutePath());
        return storageFolder.getAbsolutePath();
    }

    public static File buildPath(File base, String... segments) {
        File cur = base;
        for (String segment : segments) {
            if (cur == null) {
                cur = new File(segment);
            } else {
                cur = new File(cur, segment);
            }
        }
        return cur;
    }
    public static String getAvaiableFilePath(Context context, String dir, String fileName) {
        dir = context.getApplicationInfo().packageName + "/" + dir;
        String filePath = "";
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filePath = Environment.getExternalStorageDirectory().getPath() + "/" + dir + "/" + fileName;
        } else
            filePath = "/sdcard/" + dir + File.separator + fileName;
        return filePath;
    }

    public static String getAvaiableFilePath(Context context, String dir) {
        dir = context.getApplicationInfo().packageName + "/" + dir;
        String filePath = "";
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filePath = Environment.getExternalStorageDirectory().getPath() + "/" + dir;
        } else
            filePath = "/sdcard/" + dir;
        return filePath;
    }

    public static boolean isExists(File file) {
        if (file.exists()) { // 判断文件是否存在
            if (file.isFile()) { // 判断是否是文件
                return true;
            }
            return false;
        }
        return false;

    }

    public static void deleteFile(File file) {
        if (file.exists()) { // 判断文件是否存在
            if (file.isFile()) { // 判断是否是文件
                file.delete(); // delete()方法 你应该知道 是删除的意思;
            } else if (file.isDirectory()) { // 否则如果它是一个目录
                File files[] = file.listFiles(); // 声明目录下所有的文件 files[];
                for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
                    deleteFile(files[i]); // 把每个文件 用这个方法进行迭代
                }
            }
            file.delete();
        }
    }
}
